package com.bilin.coin.activity;

import android.graphics.Paint;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bilin.coin.R;
import com.bilin.coin.bean.CommonSuccessEvent;
import com.bilin.coin.bean.FlBiLinBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.tools.Geter;
import com.bilin.coin.tools.Poster;
import com.bilin.coin.utils.DensityUtils;
import com.bilin.coin.utils.GsonUtil;
import com.bilin.coin.utils.SpacesItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suke.widget.SwitchButton;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import kt.com.bilin.coin.ad.AdManager;
import kt.com.bilin.coin.ad.FusionAdManager;

@Route(path = "/auction/CouponActivity")
public class CouponActivity extends BaseAppCompatActivity {

    @BindView(R.id.switch_button)
    SwitchButton switchButton;
    @BindView(R.id.tv_num)
    TextView tv_num;
    @BindView(R.id.tv_limit_time)
    TextView tv_limit_time;
    @BindView(R.id.tv33)
    TextView tv33;
    @BindView(R.id.tv333)
    TextView tv333;
    @BindView(R.id.tv3333)
    TextView tv3333;
    @BindView(R.id.bt3)
    TextView bt3;
    @BindView(R.id.tv_usdt_amount)
    TextView tv_usdt_amount;

    @BindView(R.id.recycle)
    RecyclerView recyclerView;

    boolean isOpen;
    boolean isFirst = true;

    FunctionGridAdapter gridAdapter;


    private FlBiLinBean flBiLinBean;

    @Override
    protected void initView() {
        //initAd();
        setTitle("比邻券");
        setMenu("比邻券明细");

        initSwitchButton();
        getData();
    }

    @Override
    public void setMenuOnClickListener() {
        super.setMenuOnClickListener();
        baseStartActivity("/auction/BilinDetailRecordActivity");
    }

    private void fillData() {
        isOpen = flBiLinBean.isOpenChildCostBlq();

        switchButton.setChecked(flBiLinBean.isOpenChildCostBlq());
        tv_num.setText(flBiLinBean.getBlqNum());
        //tv_limit_time.setText(flBiLinBean.getBlqNum());
        //bt3.setEnabled(!flBiLinBean.isIsAdOver());
        bt3.setText(!flBiLinBean.isIsAdOver() ? "去完成" : "已完成");
        tv33.setText("累计完成" + flBiLinBean.getTimes() + "/" + flBiLinBean.getPerDayTimes());
        tv333.setText(flBiLinBean.getAdAwardStr1());
        tv3333.setText(flBiLinBean.getAdAwardStr2());

        tv_usdt_amount.setText("我的USDT余额：" + flBiLinBean.getU());
        initAdapter();

    }

    @OnClick({R.id.bt_confirm, R.id.bt3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_confirm:
                confirm();
                break;
            case R.id.bt3:
                showAd();
                break;
        }
    }

    private void confirm() {
        if (gridAdapter == null) {
            showToast("请稍候");
            return;
        }
        List<FlBiLinBean.BlBlqMealBean> data = gridAdapter.getData();
        FlBiLinBean.BlBlqMealBean selected = null;
        for (FlBiLinBean.BlBlqMealBean blBlqMealBean : data) {
            if (blBlqMealBean.isCheck()) {
                selected = blBlqMealBean;
            }
        }
        if (selected == null) {
            showToast("请先选择套餐");
            return;
        }
        String id = selected.getId() + "";
        new Poster(this, true, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-ad-fl/exchange";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("id", id);
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                EventBus.getDefault().post(new CommonSuccessEvent(CommonSuccessEvent.DuiHuanSuccess));
                getData();
                //finish();
            }
        };
    }

    private void initAdapter() {
        if (gridAdapter == null) {
            gridAdapter = new FunctionGridAdapter(flBiLinBean.getBlBlqMeal());
            recyclerView.setLayoutManager(fillLayoutManger());
            recyclerView.setAdapter(gridAdapter);
            gridAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    FlBiLinBean.BlBlqMealBean item = (FlBiLinBean.BlBlqMealBean) adapter.getData().get(position);
                    item.setCheck(!item.isCheck());
                    List<FlBiLinBean.BlBlqMealBean> data = adapter.getData();
                    for (FlBiLinBean.BlBlqMealBean blBlqMealBean : data) {
                        if (item.getId() != blBlqMealBean.getId()) {
                            if (item.isCheck()) {
                                blBlqMealBean.setCheck(!item.isCheck());
                            }
                        }
                    }
                    //gridAdapter.setNewData(data);
                    gridAdapter.notifyDataSetChanged();
                }
            });
        } else {
            gridAdapter.setNewData(flBiLinBean.getBlBlqMeal());
        }

    }

    private RecyclerView.LayoutManager fillLayoutManger() {
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        manager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.addItemDecoration(new SpacesItemDecoration(3, DensityUtils.dp2px(this, 10), 0x1642));
        return manager;
    }


    private void getData() {
        new Geter(this, false, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-ad-fl/getData";
            }

            @Override
            protected Map<String, String> fillParams() {
                return new HashMap<>();
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                flBiLinBean = GsonUtil.GsonToBean(dataJson, FlBiLinBean.class);
                fillData();
            }

        };
    }

    private void initSwitchButton() {
        switchButton.toggle();     //switch state
        switchButton.toggle(true);//switch without animation
        switchButton.setShadowEffect(true);//disable shadow effect
       /* switchButton.setEnabled(false);//disable button
        switchButton.setEnableEffect(false);//disable the switch animation*/
        switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                if (flBiLinBean != null) {
                    if (isFirst && flBiLinBean.isOpenChildCostBlq()) {
                        //openOrClose();
                    } else {
                        openOrClose();
                    }
                }
                isFirst = false;
            }
        });
       /* switchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flBiLinBean != null) {
                    openOrClose();
                }
            }
        });*/
    }

    private void openOrClose() {

        new Poster(this, true, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-ad-fl/openOrClose";
            }

            @Override
            protected Map<String, String> fillParams() {
                return new HashMap<>();
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                isOpen = !isOpen;
            }
        };
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_coupon;
    }

    class FunctionGridAdapter extends BaseQuickAdapter<FlBiLinBean.BlBlqMealBean, BaseViewHolder> {

        public FunctionGridAdapter(@Nullable List<FlBiLinBean.BlBlqMealBean> data) {
            super(R.layout.item_tao_can, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, FlBiLinBean.BlBlqMealBean item) {

            TextView tv_price2 = helper.getView(R.id.tv_price2);
            TextView tv_num = helper.getView(R.id.tv_num);
            TextView tv_price1 = helper.getView(R.id.tv_price1);
            LinearLayout ll_bg = helper.getView(R.id.ll_bg);
            helper.setText(R.id.tv_num, item.getGainAmount() + "张比邻券");
            helper.setText(R.id.tv_price1, item.getCostUReal() + "U");
            helper.setText(R.id.tv_price2, item.getCostU() + "U");
            if (item.isCheck()) {
                ll_bg.setBackgroundResource(R.mipmap.bg_bilin_f);
                tv_num.setTextColor(getResources().getColor(R.color.whiteMain));
                tv_price1.setTextColor(getResources().getColor(R.color.whiteMain));
                tv_price2.setTextColor(getResources().getColor(R.color.whiteMain));
            } else {
                ll_bg.setBackgroundResource(R.mipmap.bg_bilin_e);
                tv_num.setTextColor(getResources().getColor(R.color.colorBlue));
                tv_price1.setTextColor(getResources().getColor(R.color.colorBlue));
                tv_price2.setTextColor(getResources().getColor(R.color.colorBlue));
            }
            tv_price2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        }
    }

    private void showAd() {
        AdManager.INSTANCE.loadRewardVideoAd(this, () -> {
            addOneAd();
            return null;
        });
    }

    private void addOneAd() {
        new Poster(this, false, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-ad-fl/addTimes";
            }

            @Override
            protected Map<String, String> fillParams() {
                return new HashMap<>();
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                getData();
            }
        };
    }
}
